// 云函数入口文件
const cloud = require('qq-server-sdk');
const md5 = require('md5');
var request = require('request-promise');
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})


exports.main = async (event, context) => {

  console.log(event);
  var xh = event.xh;
  var term = event.term;
  var week = event.week;
  var schoolYear = event.schoolYear;
  if (xh == undefined || week == undefined || xh == '' || xh == null) {
    return event
  }
  if (schoolYear == undefined) {
    schoolYear = '2020-2021';
  }
  if (term == undefined) {
    term = 2;
  }

  // 下列代码，请谨慎使用！！
  var timestamp = (new Date()).valueOf();
  var orginalData =
    '/normalservice/course/selectCourse?appKey=9b5e431b1a154e20a93357c9c0b732e4&clientCategory=PC&isWeek=YES&nonce=' +
    timestamp + '&operatorId=' +
    xh + '&schoolYear=' +
    schoolYear + '&term=' +
    term + '&timestamp=' +
    timestamp + '&universityId=102574&userType=STUDENT&week=' + week;
  var secr = '&appSecret=637208d1c1404cf4ad943f313ce21cd3';
  var md5str = md5(orginalData + secr).toUpperCase();
  const kk = await request('http://ai.cqvie.edu.cn' + orginalData + '&sign=' + md5str).then(
    function (res) {
      return res;
    })
    .catch(function (err) {
      return temp;
    });
  return kk;
};

